#include<arpa/inet.h>
#include<stdlib.h>
#include<iostream>
#include<stdio.h>

#define SIZE 4096

using namespace  std;

void close_connection( int client_socket) {
		shutdown(client_socket, SHUT_RDWR);
		close(client_socket);
}



int talk_with_server(int socket) {
	char buffer[SIZE];
	while(true)
	{	
		cin.getline(buffer, SIZE);
		int size = 0;
		size = send(socket, buffer,SIZE,0);
		if(size <= 0) {
			if(size < 0)
				perror("Receive: ");
			//close_connection(socket);
				return -1;
		} else {	
			size = recv(socket,buffer,size,0);
			if(size < 0) {
				perror("Send: ");
				//close_connection(socket);
				return -1;
			}
			cout << "From server:"<<buffer << endl;
		}
	}
return 0;
}


int main(int argc, char** argv)
{
	cout << "Echo client" << endl;
    char recv_buf[SIZE];
	char buf[SIZE];
	char req[SIZE];
	in_addr host;
	inet_aton(argv[1], &host);
	int remote_server_sckt = socket(AF_INET, SOCK_STREAM, 0);
	sockaddr_in *remote_server = new sockaddr_in;
	remote_server->sin_family = AF_INET;
	remote_server->sin_addr.s_addr = host.s_addr; //(errcheck = inet_addr(host_name));
	remote_server->sin_port = htons(atoi(argv[2]));
	unsigned int a =  sizeof(sockaddr_in);
	
	int errcheck = connect(remote_server_sckt,(sockaddr*)remote_server,sizeof(sockaddr_in));
	talk_with_server(remote_server_sckt);
	return 0;
}
